package cn.itcast.service;

import cn.itcast.interfaces.OrderSettingService;
import cn.itcast.mapper.OrderSettingMapper;
import cn.itcast.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import io.reactivex.Observable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/14 9:06
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    /**
     * 批量导入预约设置人数
     * @param orderSettings
     */
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if(CollectionUtils.isEmpty(orderSettings) && orderSettings.size()>0) {
            //遍历，循环添加预约设置
          /*  for (OrderSetting orderSetting : orderSettings) {
                //查询判断该日期是否已设置可预约人数
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    //该日期已设置可预约人数，更新即可
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }
                //该日期未设置可预约人数
                orderSettingMapper.add(orderSetting);
            }*/
          //Rxjava遍历
            Observable.fromIterable(orderSettings).subscribe(orderSetting -> {
                //查询判断该日期是否已设置可预约人数
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    //该日期已设置可预约人数，更新即可
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }
                //该日期未设置可预约人数
                orderSettingMapper.add(orderSetting);
            });
        }
    }

    /**
     * 获取本月预约设置数据
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接获取本月份的第一天和最后一天
        String beginDate = date + "-1";
        String endDate = date + "-31";
        HashMap<String, Object> map = new HashMap<>();
        map.put("beginDate",beginDate);
        map.put("endDate",endDate);
        List<OrderSetting> orderSettingList = orderSettingMapper.findOrderSettingByMonth(map);
        ArrayList<Map> orderList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(orderSettingList) && orderSettingList.size()>0){
            /*for (OrderSetting orderSetting : orderSettingList) {
                Map<String, Object> hashMap = new HashMap<>();
                int orderDate = orderSetting.getOrderDate().getDate();  //获取当天日期
                int number = orderSetting.getNumber();   //获取可预约人数
                int reservations = orderSetting.getReservations();  //获取已预约人数
                hashMap.put("date",orderDate);
                hashMap.put("number",number);
                hashMap.put("reservations",reservations);
                orderList.add(hashMap);
            }*/
            //Rxjava遍历
            Observable.fromIterable(orderSettingList).subscribe(orderSetting -> {
                Map<String, Object> hashMap = new HashMap<>();
                int orderDate = orderSetting.getOrderDate().getDate();  //获取当天日期
                int number = orderSetting.getNumber();   //获取可预约人数
                int reservations = orderSetting.getReservations();  //获取已预约人数
                hashMap.put("date",orderDate);
                hashMap.put("number",number);
                hashMap.put("reservations",reservations);
                orderList.add(hashMap);
            });
        }
        return orderList;
    }

    /**
     * 页面设置可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //首先确定该预约日期是否已设置可预约人数
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //已设置页面可预约人数，更新修改即可
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else {
            //未设置可预约人数，添加即可
            orderSettingMapper.add(orderSetting);
        }
    }
}
